package com.platform.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.entity.AgentEntity;
import com.platform.service.AgentService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

/**
 * 商品对应规格表值表Controller
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2018-10-07 20:51:28
 */
@RestController
@RequestMapping("agent")
public class AgentController extends AbstractController {
    @Autowired
    private AgentService agentService;

    /**
     * 查看供应商列表
     */
    @RequestMapping("/agentList")
    @RequiresPermissions("agent:list")
    public R agentList(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        //供应商ID
        query.put("rootDeptId", getRootDept().getDeptId());

        List<AgentEntity> agentList = agentService.queryAgentList(query);
        int total = agentService.queryAgentTotal(query);

        PageUtils pageUtil = new PageUtils(agentList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看供应商列表
     */
    @RequestMapping("/vendorList")
    @RequiresPermissions("vendor:list")
    public R vendorList(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        //供应商ID
        query.put("rootDeptId", getRootDept().getDeptId());

        List<AgentEntity> agentList = agentService.queryVendorList(query);
        int total = agentService.queryVendorTotal(query);

        PageUtils pageUtil = new PageUtils(agentList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("agent:info")
    public R info(@PathVariable("id") Integer id) {
        AgentEntity agent = agentService.queryObject(id);

        return R.ok().put("agent", agent);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("agent:save")
    public R save(@RequestBody AgentEntity agent) {
        agentService.save(agent);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("agent:update")
    public R update(@RequestBody AgentEntity agent) {
        agentService.update(agent);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("agent:delete")
    public R delete(@RequestBody Integer[] ids) {
        agentService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
//    @RequestMapping("/queryAll")
//    public R queryAll(@RequestParam Map<String, Object> params) {
//
//        List<AgentEntity> list = agentService.queryList(params);
//
//        return R.ok().put("list", list);
//    }

    /**
     * 否决代理申请
     */
    @RequestMapping("/rejectApply")
    public R rejectApply(@RequestBody Integer[] ids) {
        agentService.rejectApply(ids, getUser().getUsername());
        return R.ok();
    }

    /**
     * 通过代理申请
     */
    @RequestMapping("/passApply")
    public R passApply(@RequestBody Integer[] ids) {
        agentService.passApply(ids, getUser().getUsername());
        return R.ok();
    }
}
